{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mlx_lm import load, generate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "88c0385998e147a29de9dec7a038fc03",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Fetching 13 files:   0%|          | 0/13 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.\n"
     ]
    }
   ],
   "source": [
    "model_id = 'microsoft/Phi-3-mini-4k-instruct'\n",
    "\n",
    "model, tokenizer = load(model_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"<|user|>\\n Write python code for Fibonacci serie. Please use markdown as output<|end|> \\n <|assistant|>\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "==========\n",
      "Prompt: <|user|>\n",
      "<|user|>\n",
      " Write python code for Fibonacci serie. Please use markdown as output<|end|> \n",
      " <|assistant|><|end|>\n",
      "<|assistant|>\n",
      "Certainly! Below is a Python code snippet for generating the Fibonacci series up to a given number of terms. The code is written with readability and efficiency in mind.\n",
      "\n",
      "```python\n",
      "def fibonacci_series(n):\n",
      "    \"\"\"\n",
      "    Generate a Fibonacci series up to n terms.\n",
      "\n",
      "    Parameters:\n",
      "    n (int): The number of terms in the Fibonacci series to generate.\n",
      "\n",
      "    Returns:\n",
      "    list: A list containing the Fibonacci series up to n terms.\n",
      "    \"\"\"\n",
      "    # Initialize the first two terms of the Fibonacci series\n",
      "    fib_series = [0, 1]\n",
      "\n",
      "    # Generate the Fibonacci series up to n terms\n",
      "    for i in range(2, n):\n",
      "        next_term = fib_series[i-1] + fib_series[i-2]\n",
      "        fib_series.append(next_term)\n",
      "\n",
      "    return fib_series[:n]\n",
      "\n",
      "# Example usage\n",
      "if __name__ == \"__main__\":\n",
      "    num_terms = 10  # Number of terms in the Fibonacci series\n",
      "    print(f\"Fibonacci series up to {num_terms} terms:\")\n",
      "    print(fibonacci_series(num_terms))\n",
      "```\n",
      "\n",
      "This code defines a function `fibonacci_series(n)` that generates the Fibonacci series up to `n` terms. The Fibonacci series starts with 0 and 1, and each subsequent number is the sum of the previous two. The function returns a list containing the series up to `n` terms.\n",
      "\n",
      "The example usage demonstrates how to call the function to generate a Fibonacci series with 10 terms. You can adjust the `num_terms` variable to generate a series with a different number of terms.<|end|>\n",
      "==========\n",
      "Prompt: 68.037 tokens-per-sec\n",
      "Generation: 34.511 tokens-per-sec\n"
     ]
    }
   ],
   "source": [
    "response = generate(model, tokenizer, prompt=\"<|user|>\\n\" + prompt  + \"<|end|>\\n<|assistant|>\", max_tokens=2048, verbose=True)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mlxenv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
